// 获取网络状态
function getNetwork() {
	let netFlag = true
	uni.getNetworkType({
	    success: function (res) {
	        if(res.networkType == 'none') {
				netFlag = false
			}
	    }
	});
	return netFlag
}

// 获取手机序列号
function getSerialNo() {
	var Build = plus.android.importClass("android.os.Build");
	var Manifest = plus.android.importClass("android.Manifest");
	var MainActivity = plus.android.runtimeMainActivity();
	var SystemProperties = plus.android.importClass("android.os.SystemProperties")
	var serial = ''
					
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { //9.0+
		var ArrPermissions = [
			Manifest.permission.READ_PHONE_STATE,
		];
		function PermissionCheck(permission) {
			if (Build.VERSION.SDK_INT >= 23) {
				if (MainActivity.checkSelfPermission(permission) == -1) {
					return false;
				}
			}
			return true;
		}
	
		function PermissionChecks(Arr) {
			var HasPermission = true;
			for (var index in Arr) {
				var permission = Arr[index];
				//如果此处没有权限,则是用户拒绝了
				if (!PermissionCheck(permission)) {
					HasPermission = false;
					break;
				}
			}
			return HasPermission;
		}
		//如果没有权限，则申请
		if (!PermissionChecks(ArrPermissions)) {
			serial = ''
		} else { //如果拥有权限，那么干点啥吧^_^
			serial = Build.getSerial()
		}
	} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) { //8.0+
		if (Build.SERIAL) {
			serial = Build.SERIAL;
		}
	} else {
		if (SystemProperties.get('ro.serialno')) {
			serial = SystemProperties.get('ro.serialno')
		}
	}
	return serial
}

function blueToothOPt() {
	const BluetoothAdapter = plus.android.importClass('android.bluetooth.BluetoothAdapter'); // 引入Java 蓝牙类
	const blueadapter = BluetoothAdapter.getDefaultAdapter(); //拿到默认蓝牙适配器方法
	
	if(blueadapter) {
		// 判断蓝牙是否开启
		if (blueadapter.isEnabled()) {
			// 已开启
			uni.showToast({
				title: '蓝牙已打开',
			})
		} else {
			// 未开启弹出提示框
			uni.showModal({
				title: '提示',
				content: '蓝牙尚未打开，是否打开蓝牙',
				showCancel: true,
				cancelText: '取消',
				confirmText: '确定',
				success(res) {
					// 点击确定后通过系统打开蓝牙
					if (res.confirm) {
						const blueadapter = BluetoothAdapter.getDefaultAdapter();
						if (blueadapter != null) {
							return blueadapter.enable();
						}
					} else {
						// 点击取消什么也不做
						console.log("点击了取消");
					}
				}
			})
		}
	}
}

// 蓝牙关闭重启
function blueToothRestart() {
	const BluetoothAdapter = plus.android.importClass('android.bluetooth.BluetoothAdapter'); // 引入Java 蓝牙类
	const blueadapter = BluetoothAdapter.getDefaultAdapter(); //拿到默认蓝牙适配器方法

	blueadapter.disable()
	setTimeout(() => {
		blueadapter.enable()
	}, 500)
}

export {
	getNetwork,
	getSerialNo,
	blueToothOPt,
	blueToothRestart
}
